package com.leo.ktvplayer.ui.chosetype;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.leo.ktvplayer.R;
import com.leo.ktvplayer.base.ActBase;
import com.leo.ktvplayer.bluetooth.BleConstant;
import com.leo.ktvplayer.bluetooth.bletask.BleEngineFactory;
import com.leo.ktvplayer.bluetooth.bletask.IBleDataEngine;
import com.leo.ktvplayer.localcookie.LocalUtils;
import com.leo.ktvplayer.ui.payment.ActPayment;
import com.leo.ktvplayer.ui.setting.ActSetting;
import com.leo.ktvplayer.ui.setting.ManagerDialog;
import com.leo.ktvplayer.util.ToastUtil;

import java.lang.reflect.Method;
import java.net.HttpURLConnection;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *
 */
public class ActChoseType extends ActBase {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_choseplaytype);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @OnClick(R.id.btn_pay)
    public void paymentt(View view) {
        Intent intent = new Intent(this, ActPayment.class);
        intent.putExtra("price", money);
        intent.putExtra("time", time);
        startActivity(intent);

    }

    @Override
    protected void onResume() {
        super.onResume();
        tv_playtim1.setText(LocalUtils.getSetting().getTime1() + "分钟");
        tv_playtim2.setText(LocalUtils.getSetting().getTime2() + "分钟");
        tv_playtim3.setText(LocalUtils.getSetting().getTime2() + "分钟");
        tv_needMoney.setText("需要支付:" + money + "元");
        rb_15min.setChecked(true);
    }

    public void init() {
        //init all listener
        rb_15min.setOnCheckedChangeListener(checkedChangeListener);
        rb_30min.setOnCheckedChangeListener(checkedChangeListener);
        rb_60min.setOnCheckedChangeListener(checkedChangeListener);
    }

    @OnClick(R.id.btn_setting)
    public void setting(View view) {
        ManagerDialog dialog = new ManagerDialog(this,R.style.dialog);
        dialog.setOnManagerDialogClickedListener((action, inputValue) -> {
            if (action == ManagerDialog.OnManagerDialogClickedListener.ACTION_POSITIVE) {
                //TODO compare userPWD
                if (inputValue.equals(LocalUtils.getUser().getUserpwd())) {
                    goToSetting();
                    dialog.cancel();
                } else if (inputValue.equals("") || inputValue == null) {
                    ToastUtil.show("密码不能为空");
                }
            }
        });
        dialog.show();
    }

    private void goToSetting() {
        Intent intent = new Intent(this, ActSetting.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    private CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            int id = buttonView.getId();
            if (id == R.id.rb_15min && isChecked) {
                money = LocalUtils.getSetting().getPrice1();
                time = 1000 * 60 * LocalUtils.getSetting().getTime1();
            } else if (id == R.id.rb_30min&& isChecked) {
                money = LocalUtils.getSetting().getPrice2();
                time = 1000 * 60 * LocalUtils.getSetting().getTime2();
            } else if (id == R.id.rb_60min&& isChecked) {
                money = LocalUtils.getSetting().getPrice3();
                time = 1000 * 60 * LocalUtils.getSetting().getTime3();
            }
            tv_needMoney.setText("需要支付:"+ money +"元");
        }
    };

    /**
     * price default value
     */
    private double money = LocalUtils.getSetting().getPrice1();

    /**
     * Time default value
     */
    private int time = 1000 * 60 * LocalUtils.getSetting().getTime1();

    @BindView(R.id.btn_setting)
    public Button btn_setting;

    @BindView(R.id.btn_pay)
    public Button btn_payment;

    @BindView(R.id.rb_15min)
    public RadioButton rb_15min;

    @BindView(R.id.rb_30min)
    public RadioButton rb_30min;

    @BindView(R.id.rb_60min)
    public RadioButton rb_60min;

    @BindView(R.id.tv_showneedmoney)
    public TextView tv_needMoney;

    @BindView(R.id.tv_activity_choseplaytyoe_time1)
    public TextView tv_playtim1;

    @BindView(R.id.tv_activity_choseplaytyoe_time2)
    public TextView tv_playtim2;

    @BindView(R.id.tv_activity_choseplaytyoe_time3)
    public TextView tv_playtim3;
}
